package gui;

import gui.util.GUIUtils;
import gui.util.TableModelAlocacao;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.Alocacao;

import com.jgoodies.forms.layout.CellConstraints;

import controller.AlocacaoController;

public class ConsultaUtilizacaoComputadoresMesGUI extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JPanel panelCabecalho;
	private JTable table;
	private TableModelAlocacao model;
	
	public JComboBox comboMes;
	public JButton botaoConsultar;
	
	public ConsultaUtilizacaoComputadoresMesGUI() {
		this.setTitle("Consulta de Softwares por Computador");
		this.setSize(600, 500);
		this.setLocationRelativeTo(null);
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setResizable(false);
	
		this.add(getPanelCabecalho(), BorderLayout.NORTH);
		this.add(new JScrollPane(getTable(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
		this.setVisible(true);
	}
	
	public JPanel getPanelCabecalho() {
		if (panelCabecalho == null){
			panelCabecalho = new JPanel(GUIUtils.getLayout(2, 18));
			CellConstraints  cc = new CellConstraints();

			panelCabecalho.add(new JLabel("Mes"),		cc.xywh(  2, 2,  7, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT ));
			panelCabecalho.add(getComboMes(),			cc.xywh( 10, 2,  5, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
			
			panelCabecalho.add(getBotaoConsultar(),		cc.xywh( 24, 2,  8, 1, CellConstraints.FILL, CellConstraints.DEFAULT ));
		}
		return panelCabecalho;
	}
	
	public JComboBox getComboMes() {
		if (comboMes == null){
			comboMes = new JComboBox();
			
			Collection<Integer> meses = null;
			try {
				meses = AlocacaoController.getInstance().buscarMesesDeAlocacao();
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (meses != null && !meses.isEmpty()){
				for (Integer mes : meses) {
					comboMes.addItem(mes);
				}
			}
		}
		return comboMes;
	}

	public JButton getBotaoConsultar() {
		if (botaoConsultar == null){
			botaoConsultar = new JButton("Consultar");
			botaoConsultar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					carregar();
				}
			});
		}
		return botaoConsultar;
	}
	
	public JTable getTable() {
		if (table == null){
			table = new JTable(getModel());
		}
		return table;
	}
	
	public TableModelAlocacao getModel(){
		if (model == null){
			model = new TableModelAlocacao();
		}
		return model;
	}
	
	private void carregar() {
		Collection<Alocacao> alocacoes = null;
		try {
			alocacoes = AlocacaoController.getInstance().buscarAlocacoesPorMes((Integer)getComboMes().getSelectedItem());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}
		getModel().setData(new Vector<Alocacao>(alocacoes));
		getModel().fireTableDataChanged();
	}


}
